<template>
  <div style="padding: 20px;">
    <h2 style="margin-bottom: 20px;">{{ isEdit ? '编辑股票净利润' : '新增股票净利润' }}</h2>
    <el-form ref="form" :model="form" :rules="rules" label-width="140px" style="max-width: 1000px;">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="股票代码" prop="stockCode">
            <el-input v-model="form.stockCode" placeholder="请输入股票代码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="会计年度" prop="fiscalYear">
            <el-input-number v-model="form.fiscalYear" placeholder="请输入会计年度" :min="2000" :max="2100" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-divider content-position="left">一季度</el-divider>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="Q1净利润" prop="q1NetProfit">
            <el-input-number v-model="form.q1NetProfit" placeholder="请输入Q1净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q1扣非净利润" prop="q1NoneNetProfit">
            <el-input-number v-model="form.q1NoneNetProfit" placeholder="请输入Q1扣非净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q1每股收益" prop="q1Eps">
            <el-input-number v-model="form.q1Eps" placeholder="请输入Q1每股收益" :precision="4" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider content-position="left">二季度</el-divider>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="Q2净利润" prop="q2NetProfit">
            <el-input-number v-model="form.q2NetProfit" placeholder="请输入Q2净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q2扣非净利润" prop="q2NoneNetProfit">
            <el-input-number v-model="form.q2NoneNetProfit" placeholder="请输入Q2扣非净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q2每股收益" prop="q2Eps">
            <el-input-number v-model="form.q2Eps" placeholder="请输入Q2每股收益" :precision="4" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider content-position="left">三季度</el-divider>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="Q3净利润" prop="q3NetProfit">
            <el-input-number v-model="form.q3NetProfit" placeholder="请输入Q3净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q3扣非净利润" prop="q3NoneNetProfit">
            <el-input-number v-model="form.q3NoneNetProfit" placeholder="请输入Q3扣非净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q3每股收益" prop="q3Eps">
            <el-input-number v-model="form.q3Eps" placeholder="请输入Q3每股收益" :precision="4" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider content-position="left">四季度</el-divider>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="Q4净利润" prop="q4NetProfit">
            <el-input-number v-model="form.q4NetProfit" placeholder="请输入Q4净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q4扣非净利润" prop="q4NoneNetProfit">
            <el-input-number v-model="form.q4NoneNetProfit" placeholder="请输入Q4扣非净利润" :precision="2" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="Q4每股收益" prop="q4Eps">
            <el-input-number v-model="form.q4Eps" placeholder="请输入Q4每股收益" :precision="4" :step="0.01" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button type="primary" @click="onSubmit" :loading="submitLoading">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "StockProfitForm",
  data() {
    return {
      form: {
        id: '',
        stockCode: '',
        fiscalYear: null,
        q1NetProfit: null,
        q1NoneNetProfit: null,
        q1Eps: null,
        q2NetProfit: null,
        q2NoneNetProfit: null,
        q2Eps: null,
        q3NetProfit: null,
        q3NoneNetProfit: null,
        q3Eps: null,
        q4NetProfit: null,
        q4NoneNetProfit: null,
        q4Eps: null
      },
      rules: {
        stockCode: [
          { required: true, message: '请输入股票代码', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        fiscalYear: [
          { required: true, message: '请输入会计年度', trigger: 'blur' },
          { type: 'number', min: 2000, max: 2100, message: '会计年度范围在 2000 到 2100', trigger: 'blur' }
        ]
      },
      isEdit: false,
      submitLoading: false
    }
  },
  created() {
    // 判断是编辑还是新增模式
    const id = this.$route.query.id;
    if (id) {
      this.isEdit = true;
      this.loadStockProfitDetail(id);
    } else {
      // 新增模式下，默认会计年度为当年
      this.form.fiscalYear = new Date().getFullYear();
    }
    this.form.id = null;
    const stockCode = this.$route.query.code;
    if (stockCode) {
      this.form.stockCode = stockCode;
    }
  },
  methods: {
    loadStockProfitDetail(id) {
      this.request.get("/stockprofit/" + id).then(res => {
        if (res.code === 200) {
          this.form = res.data;
        }
      }).catch(() => {
        this.$message.error('加载数据失败');
      });
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          this.request.post('/stockprofit', this.form).then(res => {
            this.submitLoading = false;
            if (res.code === 200) {
              this.$message.success(this.isEdit ? '更新成功' : '添加成功');
              this.$router.push({ name: 'StockProfit' });
            }
          }).catch(() => {
            this.submitLoading = false;
            this.$message.error('保存失败');
          });
        } else {
          this.$message.warning('请填写完整的表单信息');
          return false;
        }
      });
    },
    onCancel() {
      this.$router.push({ name: 'StockProfit' });
    }
  }
}
</script>

<style scoped>
</style>